using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodePlex.SpaceBlock;
using System.Diagnostics;
using CodePlex.SpaceBlock.FS;
using CodePlex.Resourceful;
using CodePlex.SpaceBlock.UI.Test;
using System.Text.RegularExpressions;
using CodePlex.SpaceBlock.JobModel;
using CodePlex.SpaceBlock.UI.Controls.Transfer;

namespace CodePlex.SpaceBlock.UI.Controls.Explore
{
    public partial class BrowserControl : CodePlex.SpaceBlock.UI.Controls.BaseControl
    {
        private static class Actions
        {

            public static readonly string Open = "Open";
            public static readonly string OpenIn = "OpenIn";

            public static readonly string Separator1 = "Separator1";

            public static readonly string Refresh = "Refresh";
            public static readonly string NewFolder = "NewFolder";
            public static readonly string Delete = "Delete";
            public static readonly string Rename = "Rename";
            public static readonly string Transfer = "Transfer";
            public static readonly string EditACL = "EditACL";
            public static readonly string Properties = "Properties";


            public static readonly string Separator2 = "Separator2";

            public static readonly string GenerateUrl = "GenerateUrl";
            public static readonly string EditServerAccessLogging = "EditServerAccessLogging";
            public static readonly string AnalyzeServerAccessLogs = "AnalyzeServerAccessLogs";
            public static readonly string EditAzureContainerAccess = "EditAzureContainerAccess";
        }


        private static PropertiesForm _propertiesForm;

        private static PropertiesForm PropertiesForm
        {
            get
            {
                return _propertiesForm;
            }
        }

  
        public event EventHandler TransferFromRequested;
        public event EventHandler TransferToRequested;
        public event EventHandler SelectedPathsChanged;

        private IFileSystem _fileSystem;
        private FoldersControl _folders;
        private FilesControl _files;
        private Control _currentFocus;
        private readonly ActionManager _actions;


        public BrowserControl()
        {
            InitializeComponent();

            _actions = new ActionManager(this, UIManager.Instance.SmallImageList, _contextMenu, _toolbar);

            _propertiesForm = new PropertiesForm(_actions);
           

            _folders = new FoldersControl();

            _currentFocus = _folders;   // default to ui default

            _folders.Dock = DockStyle.Fill;

            _splitContainer.Panel1.Controls.Add(_folders);

            _files = new FilesControl();


            EventHandler bubbleTransferToRequested = delegate
            {
                this.OnTransferToRequested();
            };

            _files.TransferToRequested += bubbleTransferToRequested;

            _folders.TransferToRequested += bubbleTransferToRequested;

            _files.Dock = DockStyle.Fill;

            _splitContainer.Panel2.Controls.Add(_files);


            _fsPicker.DropDownItemClicked += delegate(object sender, ToolStripItemClickedEventArgs e)
            {
                this.SelectFileSystem(e.ClickedItem.Tag as IFileSystem,true);
            };
        }


        public void Initialize(FileSystemManager manager, int preferredFileSystem)
        {
            _fsPicker.DropDownItems.Clear();
            _fsPicker.AutoSize = true;

            foreach (IFileSystem system in manager.FileSystems)
            {
                
                ToolStripMenuItem item = new ToolStripMenuItem();

                item.Text = system.ShortCaption;
                item.ToolTipText = system.Caption;
                item.Tag = system;

                if (system is LocalFileSystem)
                {
                    item.Image = UIManager.Instance.GetSmallImage(UIConstants.ImageHints.Drive);
                }
                if (system is S3FileSystem)
                {
                    item.Image = UIManager.Instance.GetSmallImage(UIConstants.ImageHints.S3);
                }
                if (system is NirvanixFileSystem)
                {
                    item.Image = UIManager.Instance.GetSmallImage(UIConstants.ImageHints.Nirvanix);
                }
                if (system is SSDSFileSystem)
                {
                    item.Image = UIManager.Instance.GetSmallImage(UIConstants.ImageHints.SSDS);
                }
                if (system is AzureBlobStorageFileSystem)
                {
                    item.Image = UIManager.Instance.GetSmallImage(UIConstants.ImageHints.Azure);
                }
                if (system is SunCloudFileSystem)
                {
                    item.Image = UIManager.Instance.GetSmallImage(UIConstants.ImageHints.SunCloud);
                }

                _fsPicker.DropDownItems.Add(item);


            }

            if (preferredFileSystem < 0)
            {
                preferredFileSystem = 0;
            }
            if (preferredFileSystem >= manager.FileSystems.Count)
            {
                preferredFileSystem = manager.FileSystems.Count - 1;
            }

            this.SelectFileSystem(manager.FileSystems[preferredFileSystem],true);
        }



        public IFileSystem FileSystem
        {
            get
            {
                return _fileSystem;
            }
        }

        private Path _pendingSelection;
        public void SelectPath(Path path)
        {
            if (!path.IsFolder) 
            {
                _pendingSelection = path;
            }
            this.SelectedFolder = path.IsFolder ? path : path.Parent;

        }

        public Path SelectedFolder
        {
            get
            {
                Path rt = null;

                this.InDisplay(delegate {
                    rt =  _folders.SelectedFolder;
                });
                return rt;
            }
            set
            {
                this.FindForm().Cursor = Cursors.WaitCursor;


                this.InBackground(delegate
                {
                    this.InDisplay(delegate
                    {
                        if (value != null)
                        {
                            _folders.SelectedFolder = value;
                        }
                    });
                },delegate
                {
                    this.InDisplay(delegate
                    {
                        if (_pendingSelection != null)
                        {
                            Path file = _pendingSelection;
                            _pendingSelection = null;

                            _files.SelectFile(file);

                        }

                        this.FindForm().Cursor = Cursors.Default;
                    });

                });
              
            }
        }

        public List<Path> SelectedPaths
        {
            get
            {
                if (_currentFocus == _files)
                {
                    return _files.SelectedFiles;
                }

                List<Path> rt = new List<Path>();
                if (_currentFocus == _folders)
                {
                    if (_folders.SelectedFolder != null)
                    {
                        rt.Add(_folders.SelectedFolder);
                    }
                }
                return rt;
            }
        }






        public void SelectFileSystem(IFileSystem fileSystem, bool selectRoot)
        {
            if (fileSystem == _fileSystem)
            {
                return;
            }

            foreach (ToolStripMenuItem item in _fsPicker.DropDownItems)
            {
                if (item.Tag == fileSystem)
                {
                    _fsPicker.Image = item.Image;
                    _fsPicker.Text = item.Text;
                    _fsPicker.ToolTipText = item.ToolTipText;
                    _fsPicker.Width = item.Width;
                    this.Select(fileSystem, selectRoot);
                    return;
                }
            }

        }

        private void Select(IFileSystem fileSystem,bool selectRoot)
        {
            _fileSystem = fileSystem;
            _folders.Initialize(this.FileSystem);
            _files.Initialize(_actions, this.FileSystem);
            _folders.Select();
            if (selectRoot)
            {
                _folders.SelectedFolder = Path.Root;
            }
        }

        private void SyncActions()
        {


            this.SyncVisible();

            if (_currentFocus == _folders)
            {
                if (_folders.SelectedFolder == null)
                {
                    return;
                }
                ISupportsCheckAction checkable = this.FileSystem as ISupportsCheckAction;
                _actions.SetEnabled(Actions.NewFolder,
                    (this.FileSystem is LocalFileSystem && _folders.SelectedFolder.Level > 0) 
                    || (this.FileSystem is S3FileSystem && _folders.SelectedFolder.Level >= 0)
                    || (this.FileSystem is NirvanixFileSystem && _folders.SelectedFolder.Level >= 0)
                    || (this.FileSystem is InMemoryFileSystem && _folders.SelectedFolder.Level >= 0)
                    || (this.FileSystem is SSDSFileSystem && _folders.SelectedFolder.Level >= 0)
                    || (checkable != null && checkable.AllowNewFolder(_folders.SelectedFolder))
                    );
                _actions.SetEnabled(Actions.Delete,
                    (this.FileSystem is LocalFileSystem && _folders.SelectedFolder.Level > 1) 
                    || (this.FileSystem is S3FileSystem && _folders.SelectedFolder.Level > 0)
                    || (this.FileSystem is NirvanixFileSystem && _folders.SelectedFolder.Level > 0)
                    || (this.FileSystem is SSDSFileSystem && _folders.SelectedFolder.Level > 0)
                    || (checkable != null && checkable.AllowDeleteFolder(_folders.SelectedFolder))
                    );

                _actions.SetEnabled(Actions.Transfer,
                    (this.FileSystem is LocalFileSystem && _folders.SelectedFolder.Level > 1) 
                    || (this.FileSystem is S3FileSystem && _folders.SelectedFolder.Level > 0)
                    || (this.FileSystem is SunCloudFileSystem && _folders.SelectedFolder.Level > 0)
                    || (this.FileSystem is NirvanixFileSystem && _folders.SelectedFolder.Level > 0)
                   
                    );
                _actions.SetEnabled(Actions.EditACL,
               ((this.FileSystem is S3FileSystem) && (_folders.SelectedFolder.Level > 0)));

                
                _actions.SetEnabled(Actions.Properties, true);
                _actions.SetEnabled(Actions.GenerateUrl, false);
                _actions.SetEnabled(Actions.EditServerAccessLogging, (this.FileSystem is S3FileSystem) && _folders.SelectedFolder.Level == 1);
                _actions.SetEnabled(Actions.AnalyzeServerAccessLogs, false);
                _actions.SetEnabled(Actions.EditAzureContainerAccess, (this.FileSystem is AzureBlobStorageFileSystem) && ((AzureBlobStorageFileSystem)this.FileSystem).IsContainer(_folders.SelectedFolder));
                _actions.SetEnabled(Actions.Open, false);
               // _actions.SetEnabled(Actions.OpenIn, false);
                _actions.SetEnabled(Actions.Rename, false);
            }

            else if (_currentFocus == _files)
            {

                _actions.SetEnabled(Actions.Open, _files.SelectedFiles.Count == 1);
               // _actions.SetEnabled(Actions.OpenIn, _files.SelectedFiles.Count == 1);

                _actions.SetEnabled(Actions.Delete, _files.SelectedFiles.Count > 0);
                _actions.SetEnabled(Actions.Rename, (this.FileSystem is ISupportsMove) && _files.SelectedFiles.Count  == 1);
                _actions.SetEnabled(Actions.Transfer, _files.SelectedFiles.Count > 0);
                _actions.SetEnabled(Actions.EditACL, (this.FileSystem is S3FileSystem) && _files.SelectedFiles.Count > 0);

                _actions.SetEnabled(Actions.Properties, _files.SelectedFiles.Count == 1);
                _actions.SetEnabled(Actions.GenerateUrl, (
                    (this.FileSystem is S3FileSystem)
                    ||(this.FileSystem is AzureBlobStorageFileSystem)
                    ||(this.FileSystem is SunCloudFileSystem))&& _files.SelectedFiles.Count == 1);
                _actions.SetEnabled(Actions.EditServerAccessLogging, false);
                _actions.SetEnabled(Actions.EditAzureContainerAccess, false);
                _actions.SetEnabled(Actions.AnalyzeServerAccessLogs, (this.FileSystem is S3FileSystem)
                    && _files.SelectedFiles.Exists(delegate(Path item) 
                    {
                        // 2007-01-02-03-15-59-79F6FA9C4977AC60    
                        return Regex.IsMatch(item.Name, @"(^\d{4}-\d{2}-\d{2})-(\d{2}-\d{2}-\d{2})-\w{16}$");
                    }));
            }
           
            this.OnSelectedPathsChanged();
        }


        private void SyncVisible()
        {
            foreach (string action in _actions.Actions)
            {
                if (Utils.In(action, Actions.EditServerAccessLogging, Actions.AnalyzeServerAccessLogs))
                {
                    _actions.SetVisible(action, this.FileSystem is S3FileSystem);
                }
                else if (Utils.In(action, Actions.GenerateUrl, Actions.Separator2))
                {
                    _actions.SetVisible(action, this.FileSystem is AzureBlobStorageFileSystem || this.FileSystem is S3FileSystem || this.FileSystem is SunCloudFileSystem);
                }
                else if (Utils.In(action,Actions.EditAzureContainerAccess))
                {
                    _actions.SetVisible(action, this.FileSystem is AzureBlobStorageFileSystem);
                }
                else
                {
                    _actions.SetVisible(action, true);
                }
            }
        }

        private void BrowserControl_Load(object sender, EventArgs e)
        {

            //_actions.Synchronizing += delegate(object sender2,CancelEventArgs e2)
            //{
            //    Control c = ((ContextMenuStrip)sender2).SourceControl as Control;

            //    bool unknownSourceControl = !(c is FilesControl2) && !(c is FoldersControl) ;

            //  //  this.SyncVisible(unknownSourceControl);
            //};

            

            _folders.FolderChanged += delegate(object sender2, EventArgs args)
            {
                _files.SelectedFolder = _folders.SelectedFolder;
                this.SyncActions();
            };


            _files.SelectedFilesChanged += delegate
            {
                this.SyncActions();
            };



            _folders.Enter += delegate
            {
                _currentFocus = _folders;
                this.SyncActions();
            };
            _files.Enter += delegate
            {
                _currentFocus = _files;
                this.SyncActions();
            };

            this.SelectedPathsChanged += delegate
            {
                PropertiesForm.Initialize(this.FileSystem, this.SelectedPaths);
            };
         























            // open
            _actions.NewAction(Actions.Open, "&Open", UIManager.Instance.GetSmallImageListIndex(UIConstants.ImageHints.Open), Keys.Control | Keys.O, delegate
             {
                 List<Path> files = _files.SelectedFiles;

                 if (files.Count != 1)
                 {
                     return;
                 }
                 Path file = files[0];



                 Operation<LocalFileSystem, Path> open = delegate(LocalFileSystem item0, Path item1)
                 {
                     string localPath = item0.ToLocalPath(item1);

                     ProcessStartInfo psi = new ProcessStartInfo();
                     psi.FileName = localPath;
                     psi.UseShellExecute = true;
                     psi.Verb = "Open";

                     try
                     {
                         Process.Start(psi);
                     }
                     catch (Win32Exception)
                     {
                         string dir = System.IO.Path.GetDirectoryName(localPath);

                         ProcessStartInfo psi2 = new ProcessStartInfo();
                         psi2.FileName = "explorer";
                         psi2.Arguments = string.Format(@"/select,""{1}""", dir, localPath);
                         psi2.UseShellExecute = true;
                         psi2.Verb = "Open";
                         Process.Start(psi2);

                     }



                 };


                 LocalFileSystem lfs = this.FileSystem as LocalFileSystem;

                 if (lfs != null)
                 {
                     open(lfs, file);
                     return;
                     
                 }



                 string tempPath = System.IO.Path.GetTempPath();
                 string dlFolder = System.IO.Path.Combine(System.IO.Path.Combine(tempPath, "SpaceBlock"), Guid.NewGuid().ToString());
                 System.IO.Directory.CreateDirectory(dlFolder);


                 string dlFile = System.IO.Path.Combine(dlFolder, file.Name);

                 lfs = AuthUtils.FileSystemManager.FileSystems.Find(delegate(IFileSystem item)
                 {
                     return item is LocalFileSystem;
                 }) as LocalFileSystem;

                 Path dlPath = lfs.FromLocalPath(dlFile);



                 TransferJob job = new TransferJob(lfs);

                 job.AddFile(
                     this.FileSystem.GetItem(file) as IFileItem,
                     dlPath, null);


                 job.Callback = delegate
                 {
                     open(lfs, dlPath);
                 };

                 Guid jobID = UIManager.Instance.JobManager.Enqueue(job);

                

             });


           // _actions.NewAction(Actions.OpenIn, "&Open in", -1,null, delegate
           //{
             


           //});





            // separator
            _actions.NewSeparator(Actions.Separator1);




            // new folder
            _actions.NewAction(Actions.NewFolder, "&New Folder...", UIManager.Instance.GetSmallImageListIndex(UIConstants.ImageHints.NewFolder), Keys.Control | Keys.N, delegate
             {

                 Control custom = null;

                 S3FileSystem s3fs = this.FileSystem as S3FileSystem;
                 string title = Properties.Messages.NewFolder_Title;
                 string description = Properties.Messages.NewFolder_Description;
                 if (s3fs != null && _folders.SelectedFolder.Level == 0)
                 {
                     NewBucketControl nbc = new NewBucketControl();
                     nbc.Initialize(s3fs.S3Connection);
                     custom = nbc;
                     title = Properties.Messages.NewBucket_Title;
                     description = Properties.Messages.NewBucket_Description;
                 }
                 
                 //this.Select(true, true);
                 string newFolderName = this.Inputbox(
                     _actions.GetIcon(UIConstants.ImageHints.NewFolder),
                     title,
                     description,
                     "Name:",
                     false,
                     custom);

                 if (newFolderName != null)
                 {
                     try
                     {
                         Path newFolder = _folders.SelectedFolder.Combine(newFolderName + Path.SeparatorChar);

                         NewBucketControl nbc = custom as NewBucketControl;
                         if (nbc != null)
                         {
                             s3fs.S3Connection.CreateBucket(newFolderName, nbc.SelectedDatacenter);
                         }
                         else
                         {
                             this.FileSystem.EnsureFolderExists(newFolder);
                         }
                         _folders.SelectedFolder = newFolder;

                     }
                     catch (Exception ex)
                     {
                         this.HandleException(ex);
                     }


                 }
             });


            // delete
            _actions.NewAction(Actions.Delete, "&Delete...", UIManager.Instance.GetSmallImageListIndex(UIConstants.ImageHints.Delete), Keys.Delete, delegate
           {

               //this.Select(true, true);

               if (_currentFocus == _folders)
               {


                   Path selectedFolder = _folders.SelectedFolder;

                   if (this.YesNo(UIManager.Instance.GetIcon(UIConstants.ImageHints.Information),
                       Properties.Messages.DeleteFolder_Title,
                       string.Format(Properties.Messages.DeleteFolder_Description, selectedFolder.Name)))
                   {
                       // OLD
                       //this.FileSystem.Delete(selectedFolder);
                       //_actions.Run(Actions.Refresh);

                       // NEW
                       DeleteJob job = new DeleteJob(this.FileSystem);
                       job.Add(selectedFolder);
                       Guid jobID = UIManager.Instance.JobManager.Enqueue(job);
                       UIManager.Instance.MainForm.ShowView<TransfersView>();
                       UIManager.Instance.MainForm.GetView<TransfersView>().ControlStrong.Select(jobID);

                   }
               }

               if (_currentFocus == _files)
               {

                   List<Path> selectedFiles = _files.SelectedFiles;

                   string title = Properties.Messages.DeleteFiles_Title;
                   string description = string.Format(Properties.Messages.DeleteFiles_Description, selectedFiles.Count);

                   if (selectedFiles.Count == 1)
                   {
                       title = Properties.Messages.DeleteFile_Title;
                       description = string.Format(Properties.Messages.DeleteFile_Description, selectedFiles[0].Name);
                   }

                   if (this.YesNo(UIManager.Instance.GetIcon(UIConstants.ImageHints.Information), title, description))
                   {

                       // OLD
                       //foreach (Path path in selectedFiles)
                       //{
                       //    this.FileSystem.Delete(path);
                       //}
                       //_actions.Run(Actions.Refresh);

                       // NEW
                       DeleteJob job = new DeleteJob(this.FileSystem);
                       foreach (Path path in selectedFiles)
                       {
                           job.Add(path);
                       }
                       Guid jobID = UIManager.Instance.JobManager.Enqueue(job);
                       UIManager.Instance.MainForm.ShowView<TransfersView>();
                       UIManager.Instance.MainForm.GetView<TransfersView>().ControlStrong.Select(jobID);


                   }
               }

           });


            // rename
            _actions.NewAction(Actions.Rename, "Rena&me...", UIManager.Instance.GetSmallImageListIndex(UIConstants.ImageHints.Rename), Keys.Control | Keys.M, delegate
             {


                 Path source = this.SelectedPaths[0] as Path;


                 RenameControl rc = new RenameControl();
                 rc.Initialize(source.Name);

                 string title = string.Format(Properties.Messages.Rename_Title,source.Name);
                 string description = Properties.Messages.Rename_Description;
                


                 //this.Select(true, true);
                 string newName = this.Inputbox(
                     _actions.GetIcon(UIConstants.ImageHints.Rename),
                     title,
                     description,
                     "Name:",
                     false,
                     rc);

                 if (rc.ValidateName(source.Name,newName))
                 {
                     try
                     {
                         Path dest = source.Parent.Combine(newName);

                         ISupportsMove moveFS = this.FileSystem as ISupportsMove;

                         moveFS.Move(source, dest);


                         _folders.SelectedFolder = dest.Parent;

                     }
                     catch (Exception ex)
                     {
                         this.HandleException(ex);
                     }


                 }
             });



            // refresh
            _actions.NewAction(Actions.Refresh, "&Refresh...", UIManager.Instance.GetSmallImageListIndex(UIConstants.ImageHints.Refresh), Keys.F5, delegate
            {
                if (_folders.SelectedFolder != null)
                {
                    _folders.SelectedFolder = _folders.SelectedFolder;
                }
            });


            // transfer
            _actions.NewAction(Actions.Transfer, "&Transfer...", UIManager.Instance.GetSmallImageListIndex(UIConstants.ImageHints.Transfer), Keys.Control | Keys.T, delegate
            {
                this.OnTransferFromRequested();
            });


            // edit acl
            _actions.NewAction(Actions.EditACL, "&Edit ACL...", UIManager.Instance.GetSmallImageListIndex(UIConstants.ImageHints.UserGroup), Keys.Control | Keys.E, delegate
            {
                ACLEditorForm form = new ACLEditorForm(this.FileSystem, this.SelectedPaths);

                form.Icon = UIManager.Instance.GetIcon(UIConstants.ImageHints.UserGroup);
                form.StartPosition = FormStartPosition.CenterScreen;

                form.FormClosed += delegate 
                {
                    if (form.DialogResult == DialogResult.OK)
                    {
                        _actions.Run(Actions.Refresh);
                    }

                    this.FindForm().Activate();
                };

                if (!form.IsDisposed)
                {
                    form.ShowDialog(this);
                }

            });



            // properties
            _actions.NewAction(Actions.Properties, "&Properties...", UIManager.Instance.GetSmallImageListIndex(UIConstants.ImageHints.Properties), Keys.Control | Keys.P, delegate
            {
               // this.Select(true, true);

                if (!PropertiesForm.Visible)
                {
                    PropertiesForm.Show(this);
                }

                PropertiesForm.Activate();

            });


            // separator
            _actions.NewSeparator(Actions.Separator2);


            // generate signed url
            _actions.NewAction(
                Actions.GenerateUrl, 
                "&Generate Url...", 
                UIManager.Instance.GetSmallImageListIndex(UIConstants.ImageHints.WorldLink), 
                Keys.Control | Keys.G, 
                delegate
            {

                GenerateSignedUrlForm dialog = new GenerateSignedUrlForm();
                dialog.Initialize(this.FileSystem, _files.SelectedFiles[0]);


                dialog.Icon = UIManager.Instance.GetIcon(UIConstants.ImageHints.WorldLink);
                dialog.StartPosition = FormStartPosition.CenterScreen;


                dialog.ShowDialog(this);

            });


            // server access logging
            _actions.NewAction(Actions.EditServerAccessLogging, "Edit &Server Access Logging...", UIManager.Instance.GetSmallImageListIndex(UIConstants.ImageHints.Note), Keys.Control | Keys.S, delegate
            {
                
                ServerAccessLoggingEditorForm dialog = new ServerAccessLoggingEditorForm();
                dialog.Initialize(this.FileSystem as S3FileSystem, this.SelectedFolder.Name);
                dialog.Icon = UIManager.Instance.GetIcon(UIConstants.ImageHints.Note);
                dialog.StartPosition = FormStartPosition.CenterScreen;
                dialog.ShowDialog(this);

            });

            
            // analyze server access logs
            _actions.NewAction(Actions.AnalyzeServerAccessLogs, "&Analyze Server Access Logs...", UIManager.Instance.GetSmallImageListIndex(UIConstants.ImageHints.NoteGo), null, delegate
            {

                ServerAccessLogViewerForm form = new ServerAccessLogViewerForm();
                form.Icon = UIManager.Instance.GetIcon(UIConstants.ImageHints.NoteGo);
                form.StartPosition = FormStartPosition.CenterScreen;
                form.Initialize(this.FileSystem as S3FileSystem, this.SelectedPaths);
                form.Show(this);
                

            });

            // edit azure container access
            _actions.NewAction(Actions.EditAzureContainerAccess, "&Edit Azure Container Access...", UIManager.Instance.GetSmallImageListIndex(UIConstants.ImageHints.PencilAndPaper), null, delegate
            {

                AzureContainerAccessEditorForm form = new AzureContainerAccessEditorForm();
                form.Icon = UIManager.Instance.GetIcon(UIConstants.ImageHints.PencilAndPaper);
                form.StartPosition = FormStartPosition.CenterScreen;
                form.Initialize(this.FileSystem as AzureBlobStorageFileSystem,this.SelectedFolder.Parts[0]);
                form.ShowDialog(this);


            });

          
            _actions.BindTo(_folders);
            _actions.BindTo(_files);

           
            this.SyncActions();
         
        }

     






        private void OnTransferFromRequested()
        {
            if (this.TransferFromRequested != null)
            {
                this.TransferFromRequested(this, EventArgs.Empty);
            }
        }

        private void OnTransferToRequested()
        {
            if (this.TransferToRequested != null)
            {
                this.TransferToRequested(this, EventArgs.Empty);
            }
        }

        private void OnSelectedPathsChanged()
        {
            if (this.SelectedPathsChanged != null)
            {
                this.SelectedPathsChanged(this, EventArgs.Empty);
            }
        }


       

    }
}

